package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-07
 * @Description: 物料主数据修改(基本视图)流程调用cwssp009新增修改料件基础资料接口
 * @ModifyLog: OA-T100，OA将物料主数据信息传给T100，T100将料号回传给OA；
 **/
public class WeaWlzsjxgsqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaWlzsjxgsqlcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();
                //审核否
                masterJson.put("conf", "Y");
                //料号  物料编码
                masterJson.put("imaa001", Util.null2String(rs.getString("MATNR")));
                //品名	物料描述
                masterJson.put("imaal003", Util.null2String(rs.getString("MAKTX")));
                //料件类型	物料类型
                masterJson.put("material_type", Util.null2String(rs.getString("MTART")));
                //规格	包装规格
                masterJson.put("imaal004", Util.null2String(rs.getString("GROES")));
                //基础单位	计量单位
                masterJson.put("imaa006", Util.null2String(rs.getString("MTART")));
                String MATKL = Util.null2String(rs.getString("MATKL"));
                //产品分类	物料组
                masterJson.put("imaa009", MATKL);
                //主分群码	物料组
                masterJson.put("imaa003", MATKL);
                //料件类别	料件类别
                masterJson.put("imaa004", Util.null2String(rs.getString("ljlb")));
                //厚度	厚度
                masterJson.put("imaaua001", Util.null2String(rs.getString("ZZGAUGE")));
                //厚度公差下限	厚度公差下限
                masterJson.put("imaaua002", Util.null2String(rs.getString("ZZGAUGE_MIN")));
                //厚度公差上限	厚度公差上限
                masterJson.put("imaaua003", Util.null2String(rs.getString("ZZGAUGE_MAX")));
                //宽度	宽度
                masterJson.put("imaaua004", Util.null2String(rs.getString("ZZWIDTH")));
                //宽度公差下限	宽度公差下限
                masterJson.put("imaaua005", Util.null2String(rs.getString("ZZWIDTH_MIN")));
                //宽度公差上限	宽度公差上限
                masterJson.put("imaaua006", Util.null2String(rs.getString("ZZWIDTH_MAX")));
                //长度	长度
                masterJson.put("imaaua007", Util.null2String(rs.getString("ZZLENGTH")));
                //长度公差下限	长度公差下限
                masterJson.put("imaaua008", Util.null2String(rs.getString("ZZLENGTH_MIN")));
                //长度公差上限	长度公差上限
                masterJson.put("imaaua009", Util.null2String(rs.getString("ZZLENGTH_MAX")));
                //内径	内径
                masterJson.put("imaaua010", Util.null2String(rs.getString("ZZINNER_DIAMETER")));
                //外径范围max	外径范围max
                masterJson.put("imaaua011", Util.null2String(rs.getString("ZZDIAMETER_MAX")));
                //外径范围min	外径范围min
                masterJson.put("imaaua012", Util.null2String(rs.getString("ZZDIAMETER_MIN")));
                //标准重量	标准重量
                masterJson.put("imaaua013", Util.null2String(rs.getString("ZZSTANDARD_WEIGHT")));
                //产品类型	产品类型
                masterJson.put("imaaua014", Util.null2String(rs.getString("ZZPRODUCT_TYPE")));
                //生命周期状态	生命周期
                masterJson.put("imaa010", Util.null2String(rs.getString("smzq")));
                //产出类型 默认：一般
                masterJson.put("imaa011", "0");
                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaWlzsjxgsqlcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.ItemCreate(payloadJson, "WeaWlzsjxgsqlcAction", requestid, src);
                writeLog(">>>>>WeaWlzsjxgsqlcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set MATNR='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaWlzsjxgsqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}